package com.aaa.gs.controller;

import com.aaa.common.entity.Product;
import com.aaa.common.util.OssUtil;
import com.aaa.common.vo.ProductOrderVO;
import com.aaa.common.vo.ProductVO;
import com.aaa.gs.service.ProductService;

import com.aaa.gs.service.RemoteProductOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import com.aaa.common.util.BaseUtil;
import com.aaa.common.vo.Page;
import com.aaa.common.util.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.multipart.MultipartFile;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2025-09-16 09:15:02
 */
@RestController
@RequestMapping("product")
@Log4j2
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;
    @Resource
    private RemoteProductOrderService remoteProductOrderService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("uploadPic")
    public Result uploadPic(@RequestParam("pic") MultipartFile file){
        //调用工具类，进行上传
        return BaseUtil.success(OssUtil.uploadFile("product/images",file));
    }

    //@PostMapping("queryByOrderId")
    //public Result queryByOrderId(@RequestParam("orderId") Integer id) {
    @GetMapping("queryByOrderId")
    public Result queryByOrderId(@RequestParam("orderId") Integer id) {
        ProductOrderVO productOrderVO = remoteProductOrderService.queryById(id);
        Integer productId = productOrderVO.getProductId();
        return BaseUtil.success(this.productService.queryById(productId));
    }

    /**
     * 修改销量
     * @param productId
     * @return
     */
    @PostMapping("updateMonthlySales")
    public int updateMonthlySales(@RequestParam("productId") Integer productId) {
        //return BaseUtil.success(this.productService.updateMonthlySales(productId));
        return this.productService.updateMonthlySales(productId);
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryProductAndProductType")
    public Result queryByPage(@RequestBody Page<ProductVO> page) {
        return BaseUtil.success(this.productService.queryByPage(page));
    }

    /**
     * 多表查询
     * @param product
     * @return
     */
    @PostMapping("frontMultiTableJointCheck")
    public Result frontMultiTableJointCheck(@RequestBody ProductVO product) {
        return BaseUtil.success(this.productService.queryAll(product));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(Integer id) {
        return BaseUtil.success(this.productService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param product 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    public Result add(@RequestBody Product product) {
        return BaseUtil.success(this.productService.insert(product));
    }

    /**
     * 编辑数据
     *
     * @param product 实体
     * @return 编辑结果
     */
    @PostMapping("update")
    public Result edit(@RequestBody ProductVO product) {
        return BaseUtil.success(this.productService.update(product));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.productService.deleteById(id));
    }

}

